//
// Created by hdf on 2020/12/10.
//

#include "WaterMarkAdder.h"

void iss::WaterMarkAdder::checkEmpty() const {
    if (srcImage.empty() || logoImage.empty()) {
        std::cerr << "image is empty" << std::endl;
        exit(0);
    }
}

cv::Mat iss::WaterMarkAdder::addWaterMarkByImage(float alpha, float beta, int Flag) const {
    checkEmpty();

    cv::Mat resImage;
    if (Flag == 1) {
        cv::Mat logoResize;
        cv::resize(logoImage, logoResize, srcImage.size());
        cv::addWeighted(srcImage, alpha, logoResize, beta, 0, resImage);
    } else {
        cv::Mat srcResize;
        cv::resize(srcImage, srcResize, logoImage.size());
        cv::addWeighted(srcResize, alpha, logoImage, beta, 0, resImage);
    }
    return resImage;
}

bool iss::WaterMarkAdder::loadSrcImage(const iss::WaterMarkAdder::String_ &imagePath) {
    srcImage = cv::imread(imagePath);
    return !srcImage.empty();
}

bool iss::WaterMarkAdder::loadLogoImage(const iss::WaterMarkAdder::String_ &imagePath) {
    logoImage = cv::imread(imagePath);
    return !logoImage.empty();
}

void
iss::WaterMarkAdder::workOnceByImage(const iss::WaterMarkAdder::String_ &src, const iss::WaterMarkAdder::String_ &logo,
                                     const iss::WaterMarkAdder::String_ &resName, float alpha, float beta, int Flag) {
    loadLogoImage(logo);
    loadSrcImage(src);
    cv::Mat res = addWaterMarkByImage(alpha, beta, Flag);
    const String_& pathAndName = resName;
    cv::imwrite(pathAndName, res);

}

void iss::WaterMarkAdder::workOnceByImageDefault(const iss::WaterMarkAdder::String_ &src,
                                                 const iss::WaterMarkAdder::String_ &logo,
                                                 const iss::WaterMarkAdder::String_ &resName) {
    workOnceByImage(src, logo, resName, 1, 0.3);
}
